package com.njsf.controller.back;

import com.github.pagehelper.PageInfo;
import com.njsf.dto.ResponseJson;
import com.njsf.pojo.Book;
import com.njsf.pojo.BookCategory;
import com.njsf.service.BookCategoryService;
import com.njsf.service.BookService;
<<<<<<< HEAD
import com.njsf.utils.DateConverter;
import com.njsf.utils.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
=======
import com.njsf.utils.UploadFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

<<<<<<< HEAD
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
=======
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/back")
public class BookController {

    @Autowired
    private BookService bookService;

    @Autowired
    private BookCategoryService bookCategoryService;

    @RequestMapping("/booklist")
    public String bookList(@RequestParam(required = false,defaultValue = "1") Integer start,
                           @RequestParam(required = false,defaultValue = "5") Integer pageSize,
<<<<<<< HEAD
                           String bookName,
                           HttpServletRequest request,
                           Map<String,Object> map) {
        if (bookName == null) {
            bookName = (String) request.getAttribute("bookName");
        }
        if (bookName != null) {
            request.setAttribute("bookName", bookName);
        }
        PageInfo<Book> pageInfo = bookService.getBooks(start, pageSize, bookName);
=======
                           Map<String,Object> map){
        PageInfo<Book> pageInfo = bookService.getBooks(start, pageSize);
>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
        map.put("pageInfo",pageInfo);
        return "back/book/book-list";
    }


    @RequestMapping("/addview")
    public String addView(Map<String, Object> map){
        List<BookCategory> bookCategories = bookCategoryService.getBookCategories();
        map.put("categories",bookCategories);
        return "back/book/book-add";
    }

    @RequestMapping("/upload")
    @ResponseBody
    public ResponseJson upload(HttpServletRequest request,MultipartFile file) throws Exception{
<<<<<<< HEAD
=======

>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
        return UploadFile.upload(request, file);
    }

    @RequestMapping("/addbook")
    @ResponseBody
<<<<<<< HEAD
    public ResponseJson addBook(Book book, String publishTime){
        book.setPublishTime(new DateConverter().convert(publishTime));
        return bookService.add(book);
    }

    @RequestMapping("/updateView")
    public String updateView(Integer id, Model model) {
        List<BookCategory> bookCategories = bookCategoryService.getBookCategories();
        model.addAttribute("categories", bookCategories);
        Book book = bookService.bookDetail(id);
        model.addAttribute("book", book);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String publishTimeFormat = sdf.format(book.getPublishTime());
        model.addAttribute("publishTimeFormat", publishTimeFormat);
        return "back/book/book-update";
    }

    @RequestMapping("/alterBook")
    @ResponseBody
    public ResponseJson alterBook(Book book, String publishTime){
        book.setPublishTime(new DateConverter().convert(publishTime));
        return bookService.alter(book);
    }

    @RequestMapping("/deleteById")
    @ResponseBody
    public ResponseJson deleteById(Integer id) {
        return bookService.deleteById(id);
    }

    @RequestMapping("/deleteList")
    @ResponseBody
    public ResponseJson deleteList(@RequestParam List<Integer> ids) {
        return bookService.deleteList(ids);
    }

    @RequestMapping("/updateChecked")
    @ResponseBody
    public ResponseJson updateChecked(Book book) {
        return bookService.alter(book);
    }
=======
    public ResponseJson addBook(Book book){
        System.out.println(book);
        return bookService.add(book);
    }

    /*更新书籍信息*/
    @RequestMapping("/showupdatebook")
    public String showUpdateBook(Map<String, Object> map,Integer id){
        List<BookCategory> bookCategories = bookCategoryService.getBookCategories();
        map.put("bookforup",bookService.bookDetail(id));
        map.put("bookCategory",bookCategories);
        return "back/book/book-update";
    }

    /*更新书籍信息*/
    @RequestMapping("/updatebook")
    @ResponseBody
    public ResponseJson saveUpdateBook(Book book){
        return bookService.updateBook(book);
    }

    /*删除书籍*/
    @RequestMapping("/delOneBook")
    @ResponseBody
    public ResponseJson delBook(Integer id){
        return bookService.delOneBook(id);
    }

>>>>>>> 180ace60693232df69575b4f82fc34aeb3fc90e8
}
